const { DataTypes } = require('sequelize');
const sequelize = require('../config/database');
const Department = require('./Department');

// 定义考生信息模型
const Candidate = sequelize.define('Candidate', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  candidateNumber: {
    type: DataTypes.STRING,
    allowNull: false,
    unique: true,
    comment: '考生编号'
  },
  name: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '姓名'
  },
  gender: {
    type: DataTypes.ENUM('male', 'female', 'other'),
    allowNull: false,
    comment: '性别'
  },
  birthDate: {
    type: DataTypes.DATE,
    allowNull: false,
    comment: '出生日期'
  },
  idCardNumber: {
    type: DataTypes.STRING,
    unique: true,
    allowNull: false,
    comment: '身份证号'
  },
  contactPhone: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '联系电话'
  },
  email: {
    type: DataTypes.STRING,
    allowNull: true,
    comment: '电子邮箱'
  },
  applicationDepartmentId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Department,
      key: 'id'
    },
    onUpdate: 'CASCADE',
    onDelete: 'RESTRICT',
    comment: '报考部门ID'
  },
  applicationMajor: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '报考专业'
  },
  educationBackground: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '学历背景'
  },
  graduationSchool: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '毕业院校'
  },
  major: {
    type: DataTypes.STRING,
    allowNull: false,
    comment: '所学专业'
  },
  status: {
    type: DataTypes.ENUM('pending', 'reviewing', 'admitted', 'rejected'),
    defaultValue: 'pending',
    comment: '申请状态'
  }
}, {
  timestamps: true,
  underscored: true,
  tableName: 'candidates',
  comment: '考生信息表'
});

// 关联部门
Candidate.belongsTo(Department, { as: 'ApplicationDepartment', foreignKey: 'applicationDepartmentId' });
Department.hasMany(Candidate, { as: 'Applicants', foreignKey: 'applicationDepartmentId' });

module.exports = Candidate;